<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>拣货任务管理</title>
		<script type="text/javascript">
	var backupTaskCode = "";
	var username = "";
	$(function() {
		$("#queryForm").validate();
		$("#_fromDate").datepicker({ format:'yyyy-mm-dd'});
		$("#_toDate").datepicker({ format:'yyyy-mm-dd'});
  });
	function initPrinter(item, arg1) {
		backupTaskCode = item;
		username = arg1;
		var indexLength = getPrinterCount();
		var tempString = "";
		for ( var i = 0; i < indexLength; i++) {
			var name = getPrinterName(i);
			tempString += "<option value='"+name+"'>" + name + "</option>";
		}
		$("#printerBody").html(tempString);
	}

	//获得打印机的个数
	function getPrinterCount() {
		LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'));
		return LODOP.GET_PRINTER_COUNT();
	};

	//根据打印机序号获得打印机名称
	function getPrinterName(iPrinterNO) {
		LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'));
		return LODOP.GET_PRINTER_NAME(iPrinterNO);
	};

	function getDate() {
		var d = new Date();
		var vYear = d.getFullYear();
		var vMon = d.getMonth() + 1;
		var vDay = d.getDate();
		var h = d.getHours();
		var m = d.getMinutes();
		var se = d.getSeconds();
		s = vYear + "-" + (vMon < 10 ? "0" + vMon : vMon) + "-"
				+ (vDay < 10 ? "0" + vDay : vDay) + " "
				+ (h < 10 ? "0" + h : h) + ":" + (m < 10 ? "0" + m : m) + ":"
				+ (se < 10 ? "0" + se : se);
		return s;
	}

	function clickPrinter() {
		ajaxPrintBackupTaskSkus();
	}

	function ajaxPrintBackupTaskSkus() {
			$.ajax({
					type : 'post',
					url : '${ctx}/focusBackupTask/ajaxPrintBackupTaskSkus',
					data : 'backupTaskCode=' + backupTaskCode,
					dataType : 'json',
					success : function(data) {
						if (data.status == "1") {
							var printName = $("#printerBody").val();
							var stringTemp = "<table width=\"100%\">";
							
							stringTemp += "<tr>";
							stringTemp += "<td width=\"33%\">去往网点：" + data.batchTask.areaGroupName + "</td>";
							stringTemp += "<td width=\"33%\">订单数量：" + data.backupTask.ordenoQty + "</td>";
							stringTemp += "<td width=\"33%\">批次类型：" + data.batchTask.type + "</td>";
							stringTemp += "</tr>";
							
							stringTemp += "<tr>";
							stringTemp += "<td width=\"33%\">拣货单号：" + backupTaskCode + "</td>";
							stringTemp += "<td width=\"33%\">姓名：" + username + "</td>";
							stringTemp += "<td width=\"33%\">生成日期：" + getDate() + "</td>";
							stringTemp += "</tr>";
							
							stringTemp += "<tr>";
							stringTemp += "<td width=\"33%\">拣货单类型："+data.batchTask.batchType+"</td>";
							stringTemp += "<td width=\"33%\">商品数量："+data.backupTask.skuQty+"</td>";
							stringTemp += "<td width=\"33%\"></td>";
							stringTemp += "</tr>";
							
							stringTemp += "</table>";

							stringTemp += "<table width='100%' border='1' cellspacing='0' style='font-size:14px;'>";
							stringTemp += "<thead><tr><th>商品编号</th><th>数量</th><th>商品信息</th><th>库位信息</th><th>备注</th></tr></thead>";
							stringTemp += "<tbody>";
							$.each(data.content,function(i) {
								stringTemp += "<tr>";
								stringTemp += "<td>"+data.content[i].skuId+"</td>";
								stringTemp += "<td>"+data.content[i].realQty+"</td>";
								stringTemp += "<td>"+data.content[i].backupTaskDTO.skuName+"</td>";
								var stockLists=data.content[i].backupTaskDTO.storeCode;
								
								if("LKE"==data.batchTask.customerCode){
									if(stockLists!=null){
										var lists=stockLists.split("<br>");
										var temp="";
										for(var j=0;j<lists.length;j++){
											var list=lists[j].split(":");
											if(""==temp){
												temp=list[0]+"——"+list[1]+"——"+list[2];
											}else{
												temp+="<br>"+list[0]+"——"+list[1]+"——"+list[2];
											}
										}
									}
									stockLists=temp;
								}
								stringTemp += "<td>"+stockLists+"</td>";
								stringTemp += "<td>"+data.content[i].backupTaskDTO.skuString+"</td>";
								stringTemp += "</tr>";
							});
							stringTemp += "</tbody></table>";
							printExpress(printName, stringTemp, backupTaskCode);
							alert("打印机加载完毕！");
						} else {
							alert(data.content);
						}

					},
					error : function() {
						alert("获取信息错误,打印失败！");
					}
				});
	}

	function printExpress(printName, content, taskName) {
		LODOP = getLodop(document.getElementById('LODOP_OB'), document.getElementById('LODOP_EM'));
		LODOP.PRINT_INIT(taskName);
		strCenterStyle = "<style/>form {text-align: center}</style>";
		LODOP.ADD_PRINT_HTM(10, 10, "100%", "100%", strCenterStyle + content);
		if (LODOP.SET_PRINTER_INDEXA(printName)) {
			LODOP.PRINT();
		}
	}

	
	//打印商品条码
	function printBarCode(width,heigth,sku,printName,stock) {
		LODOP.PRINT_INIT(sku);
		LODOP.SET_PRINT_PAGESIZE(1,width,heigth,""); 
		var strCenterStyle = "<style> .font{font-size:20px} </style>";
		LODOP.ADD_PRINT_HTM(20,5,"100%","80%",strCenterStyle+stock);
		LODOP.ADD_PRINT_BARCODE(400, 20, 400, 50, "128A", sku);
		if (LODOP.SET_PRINTER_INDEXA(printName)) {
			var item=LODOP.PRINT(); 
		}
	}
	
	function initPrinterSKU(item) {
		backupTaskCode = item;
		var indexLength = getPrinterCount();
		var tempString = "";
		for ( var i = 0; i < indexLength; i++) {
			var name = getPrinterName(i);
			tempString += "<option value='"+name+"'>" + name + "</option>";
		}
		$("#printerSKUBody").html(tempString);
	}
	
	function clickPrinterSKU() {
		var skuSize=$("#skuSize").val();
		$.ajax({
				type : 'post',
				url : '${ctx}/focusBackupTask/ajaxPrintBackupTaskSkus',
				data : 'backupTaskCode=' + backupTaskCode,
				dataType : 'json',
				success : function(data) {
					if (data.status == "1") {
						var lists=skuSize.split("X");
						var printName = $("#printerSKUBody").val();
						$.each(data.content,function(i,obj) {
							var sku=obj.skuId;
							var skuName=obj.backupTaskDTO.skuName;
							var stockLists=data.content[i].backupTaskDTO.storeCode;
							if(stockLists!=null){
								var number=0;
								var lists=stockLists.split("<br>");
								var temp="<table width='100%' border='1' class='font'><thead><th>库位</th><th>箱子编码</th><th>数量</th></thead>";
								    temp+="<tbody>";
								for(var j=0;j<lists.length;j++){
									var list=lists[j].split(":");
									number=Number(list[2])+number;
									temp+="<tr>";
									temp+="<td>"+list[0]+"</td>";
									temp+="<td>"+list[1]+"</td>";
									temp+="<td>"+list[2]+"</td>";
									temp+="</tr>";
								}
								temp+="<tr><td colspan='3'>商品总数量:"+number+"</td></tr>";
								temp+="<tr><td colspan='3'>"+skuName+"</td></tr>";
								temp+="<tr><td colspan='3'>SKU:"+sku+"</td></tr>";
								temp+="</tbody>";
								temp+="</table>";
								stockLists=temp;
							}
							printBarCode(lists[0],lists[1],sku,printName,stockLists);
						});
						alert("打印机加载完毕！");
					} else {
						alert(data.content);
					}

				},
				error : function() {
					alert("获取信息错误,打印失败！");
				}
			});
  }
	
	
	 function ajaxAreaGroup(item) {
			customerCode=item.value;
			$.ajax({
						type : 'post',
						url : '${ctx}/areaGroup/ajaxAreaGroup',
						data : 'customerCode=' + customerCode,
						dataType : 'json',
						success : function(data) {
						  var tempString="";
							if (data.status == "1") {
								$.each(data.content,function(i){
								    tempString+="<option value="+data.content[i].groupCode+">"+data.content[i].groupName;
								});
							} else if (data.status == "2") {
								$.each(data.content.restAreaGroupDtos,function(i){
								    tempString+="<option value="+data.content.restAreaGroupDtos[i].areagroupCode+">"+data.content.restAreaGroupDtos[i].areagroupName;
								});
							}else {
								alert(data.content);
							}
	                    $("#_warehouseCode").html(tempString);
	                    $("#_warehouseCode").trigger("chosen:updated");
						},
						error : function() {
							alert("获取信息错误！");
						}
					});
		}
	 
	 function ajaxHomeAreaGroup(item) {
			customerCode=item.value;
			$.ajax({
						type : 'post',
						url : '${ctx}/areaGroup/ajaxHomeAreaGroup',
						data : 'customerCode=' + customerCode,
						dataType : 'json',
						success : function(data) {
						  var tempString="";
							if (data.status == "1") {
								$.each(data.content,function(i){
								    tempString+="<option value="+data.content[i].groupCode+">"+data.content[i].groupName;
								});
							}else {
								alert(data.content);
							}
	                    $("#areaGroupCodeSingle").html(tempString);
	                    $("#areaGroupCodeSingle").trigger("chosen:updated");
						},
						error : function() {
							alert_message("系统错误！","请检查网络连接是否正常！","error",10000);
						}
					});
		}
</script>
	</head>
	<body>
		<div id="message" class="alert alert-success" style="display: none;">
			<span id="content"></span>
		</div>
		
		
		
		<form class="form-search" id="queryForm" method="post" action="${ctx}/focusBackupTask/queryBackupTask" style="margin-top: 10px;">
	     <table class="table table-striped table-bordered table-condensed" style="margin-top: 0px;">
		<thead>
			<th style="vertical-align: middle; color: white; " colspan="1" bgcolor="#999">条件查询</th>
		</thead>
		<tr >
		   <td>
		      <label style="margin-left: 10px;"><b>商家:</b></label>
				<select class="input-small required"  id="_customerCode" name="_customerCode"  value="${param._customerCode}" style="width: 95px;" onchange="ajaxHomeAreaGroup(this);ajaxAreaGroup(this);">
						<c:forEach items="${customers}" var="o">
							<option value="${o.customerCode}" <c:if test="${param._customerCode eq o.customerCode }">selected=selected</c:if>>${o.customerName}</option>
						</c:forEach>
				</select> 
 			 	<label style="margin-left: 10px;"><b>仓库:</b></label> 
 				 <select  name="_warehouseCode"  id="_warehouseCode" value="${param._warehouseCode}"  class="input-small" style="width: 120px;"> 
					
 					<c:if test="${wmList eq null }">
							<c:forEach items="${object.restAreaGroupDtos}" var="obj">
								<option value="${obj.areagroupCode}" <c:if test="${areaGroupCode eq obj.areagroupCode}">selected</c:if>> ${obj.areagroupName}
								</option>
							</c:forEach>
				   </c:if>
				   <c:if test="${object eq null }">
				     <c:forEach items="${wmList}" var="wm">
						<option value="${wm.groupCode}" <c:if test="${param._warehouseCode eq wm.groupCode}">selected=selected</c:if> >${wm.groupName}</option>
					  </c:forEach>
				   </c:if>
 				</select>
				 <label><b>网点:</b></label>
						<select name="_areaGroupCode" class="input-small" style="width: 160px;" id="areaGroupCodeSingle">
							<c:forEach items="${areaGroups}" var="obj">
								<option value="${obj.groupCode}" <c:if test="${areaGroupCode eq obj.groupCode}">selected</c:if>>${obj.groupName}
								</option>
							</c:forEach>
						</select>
				 <label style="margin-left: 10px;"><b>类型:</b></label>
				 <select class="input-medium"  id="_batchType" name="_batchType" style="width: 70px;">
				        <option value="" <c:if test="${param._pickType eq ''}">selected="selected"</c:if>>全部</option>
				        <option value="single" <c:if test="${param._pickType eq 'single'}">selected="selected"</c:if>>单件</option>
				        <option value="multiple" <c:if test="${param._pickType eq 'multiple'}">selected="selected"</c:if>>多件</option>
				 </select>
			 	
				
			    
		   </td>
		</tr>
		<tr>
		<td>
		<label style="margin-left: 10px;"><b>时间：</b></label> 
			 	<input type="text" name="_fromDate" id="_fromDate"  class="input-medium" value="${param._fromDate}" style="margin-left: -10px;width: 80px;" >
			 	<label><b>至：</b></label> 
			 	<input type="text" name="_toDate" id="_toDate" class="input-medium" value="${param._toDate}" style="margin-left: -10px;width: 80px;" >
			 	
		   
		        <button class="btn btn-info" type="submit" style="margin-left: 10px;">搜索</button>
		</td>
		</tr>
		
	</table>
	 </form>
		<table id="contentTable"
			class="table table-striped table-bordered table-condensed">
			<thead>
				<tr>
					<th>
						编号
					</th>
					<th>
						订单
					</th>
					<th>
						商品
					</th>
					<th>
						SKU
					</th>
					<th>
						类型
					</th>
					
					<th>
						操作人
					</th>
					<th>
						状态
					</th>
					<th>
						操作
					</th>
				</tr>
			</thead>
			<c:if test="${status eq 1 }">
				<tbody>
					<c:forEach items="${backupTasks}" var="obj">
						<tr>
							<td>
								${obj.backupTaskCode }
							</td>
							<td>
								<a href="${ctx }/focusBackupTask/showOrdenos/${obj.backupTaskCode}">${obj.ordenoQty}</a>

							</td>
							<td>
								${obj.skuQty }
							</td>
							<td>
								${obj.skuType }
							</td>
							<td>
								<c:if test="${obj.type eq 'single' }">
							       <span class="badge badge-info">  单件</span>
							</c:if>
								<c:if test="${obj.type eq 'multiple' }">
							       <span class="badge badge-inverse">  多件</span>
							</c:if>
								
							</td>
							
							<td>
								${obj.username }
							</td>
							
							<td>

								<c:choose>
									<c:when test="${obj.type eq 'forward' }">
										<span class="label label-success"> 打单</span>
									</c:when>
									<c:otherwise>
										<c:if test="${obj.pickStatus eq 'finish' }">
											<span class="badge badge-success">拣货完成</span>
										</c:if>
										<c:if test="${obj.pickStatus eq 'pending' }">
											<span class="badge">等待拣货</span>
										</c:if>
									</c:otherwise>
								</c:choose>

							</td>
							<td>
							<c:if test="${obj.pickType ne 'picking' }">
								<a class="btn btn-success btn-small" href="${ctx}/pickBackupTask/toPickPage/${obj.backupTaskCode}">边拣边装</a>
								</c:if>
								<c:choose>
									<c:when test="${obj.type eq 'forward' }">
										<a href="${ctx }/printBackupTask/printInit/${obj.backupTaskCode}" class="btn btn-primary">打印订单</a>
									</c:when>
									
									<c:otherwise>
										<c:if test="${obj.pickStatus eq 'pending' }">
										  <c:if test="${obj.pickType ne 'pickBox' }">
											<a href="${ctx }/focusBackupTask/pickingSku/${obj.backupTaskCode}" class="btn btn-info btn-small">电脑拣货</a>
										  </c:if>
										</c:if>
										<a href="#selextPrinter" role="button" data-toggle="modal" class="btn btn-primary btn-small" onclick="initPrinter('${obj.backupTaskCode}','${obj.username }')">打印拣货单</a>
									</c:otherwise>
								</c:choose>
								
								<a class="btn btn-small btn-warning" href="${ctx}/focusBackupTask/exportPickExcel/${obj.backupTaskCode}">导出</a>
								<a href="#printerSKU" role="button" data-toggle="modal" class="btn btn-primary btn-small" onclick="initPrinterSKU('${obj.backupTaskCode}')">SKU打印</a>
								
							</td>
						</tr>
					</c:forEach>
				</tbody>
			</c:if>
			<c:if test="${status eq 0 }">
				<tbody>
					<c:forEach items="${backupTasks.content}" var="obj">
						<tr>
							<td>
								${obj.backupTaskCode }
							</td>
							<td>
								<a href="${ctx }/focusBackupTask/showOrdenos/${obj.backupTaskCode}">${obj.ordenoQty}</a>
							</td>
							<td>
								${obj.skuQty }
							</td>
							<td>
								${obj.skuType }
							</td>
							<td>
								<c:if test="${obj.type eq 'single' }">
							  <span class="badge badge-info">  单件</span>
							</c:if>
								<c:if test="${obj.type eq 'multiple' }">
							  <span class="badge badge-inverse">  多件</span>
							</c:if>
								
							</td>
							
							<td>
								${obj.username }
							</td>
							
							<td>
								
									<c:if test="${obj.pickStatus eq 'finish' }">
										<span class="badge badge-success">拣货完成</span>
									</c:if>
									<c:if test="${obj.pickStatus eq 'pending' }">
										<span class="badge">等待拣货</span>
									</c:if>
								
							</td>
							<td>
							<c:if test="${obj.pickStatus ne 'finish' }">
								   <c:if test="${obj.pickType ne 'picking' }">
								     <a class="btn btn-success btn-small" href="${ctx}/pickBackupTask/toPickPage/${obj.backupTaskCode}">边拣边装</a>
								   </c:if>
								</c:if>
								<c:choose>
									<c:when test="${obj.type eq 'forward' }">
										<a
											href="${ctx }/printBackupTask/printInit/${obj.backupTaskCode}"
											class="btn btn-primary">打印订单</a>
									</c:when>
									<c:otherwise>
										<c:if test="${obj.pickStatus ne 'finish' }">
										  <c:if test="${obj.pickType ne 'pickBox' }">
											<a href="${ctx }/focusBackupTask/pickingSku/${obj.backupTaskCode}" class="btn btn-info btn-small">电脑拣货</a>
										  </c:if>
										</c:if>
										<a href="#selextPrinter" role="button" data-toggle="modal" class="btn btn-primary btn-small" onclick="initPrinter('${obj.backupTaskCode}','${obj.username }')">打印拣货单</a>
									</c:otherwise>
								</c:choose>
								
								
								<a class="btn btn-small btn-warning" href="${ctx}/focusBackupTask/exportPickExcel/${obj.backupTaskCode}">导出</a>
								<a href="#printerSKU" role="button" data-toggle="modal" class="btn btn-primary btn-small" onclick="initPrinterSKU('${obj.backupTaskCode}')">SKU打印</a>
							</td>
						</tr>
					</c:forEach>
				</tbody>
			</c:if>
		</table>
		<!-- 打印拣货单 -->
		<div style="display: none; width: 1000" class="modal hide fade"
			tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
			aria-hidden="true" id="selextPrinter">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"
					aria-hidden="true">
					×
				</button>
				<h3 id="printerNumber">
					选择打印机
				</h3>
			</div>
			<div class="modal-body">
				<select id="printerBody" name="printerBody">
				</select>
			</div>
			<div class="modal-footer">

				<button class="btn btn-primary" data-dismiss="modal" aria-hidden="true"
					id="printer" onclick="clickPrinter()">
					开始打印
				</button>

				<button class="btn" data-dismiss="modal" aria-hidden="true">
					关闭
				</button>
			</div>
		</div>
		
		<!-- 打印SKU -->
		<div style="display: none; width: 1000" class="modal hide fade"
			tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
			aria-hidden="true" id="printerSKU">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
					×
				</button>
				<h3>
					打印拣货SKU条码
				</h3>
			</div>
			<div class="modal-body">
			  <div>
			    <label> 打印机:</label>
			
				<select id="printerSKUBody" name="printerSKUBody">
				</select>
			  </div>
			   <div>
			    <label>  纸张尺寸:</label>
			
				<select id="skuSize" name="skuSize">
				  <option value="2030X1000">(10.0)X(20.3)(cm)
				</select>
			  </div>
			</div>
			<div class="modal-footer">

				<button class="btn btn-primary" data-dismiss="modal" aria-hidden="true"  onclick="clickPrinterSKU()">
					开始打印
				</button>

				<button class="btn" data-dismiss="modal" aria-hidden="true">
					关闭
				</button>
			</div>
		</div>
		
		<c:if test="${status eq 0 }">
			<tags:pagination page="${backupTasks}" paginationSize="5" />
		</c:if>
	</body>
</html>